<?php
namespace Macmanage\Controller;
use Common\Controller\AdminbaseController;

class MlabadminController extends AdminbaseController{
		function _initialize() {
			parent::_initialize();
							
		}
		function index(){
			$mlab_model = D('Lab');
			$count = $mlab_model->count();
			$Page  = $this->Page($count,10);  // 实例化分页类 传入总记录数和每页显示的记录数(15)
			$show  = $Page->show("Admin");   // 分页显示输出
			$lab_lists  = $mlab_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
			$this->assign('lab_lists',$lab_lists);  // 赋值数据集
			$this->assign('Page',$show);   // 赋值分页输出	
			$this -> display();
		}
		
		function add(){		
			$this->display();		
		}
		function add_post(){
			$mlab_model = D('Lab');
			if(IS_POST){
				if(!empty($_POST['photos_alt']) && !empty($_POST['photos_url'])){
				foreach ($_POST['photos_url'] as $key=>$url){
					$photourl=sp_asset_relative_url($url);
					$_POST['smeta']['photo'][]=array("url"=>$photourl,"alt"=>$_POST['photos_alt'][$key]);
				}
				}				
				$data=I("post.");  //获取所有post数据的正确写法
				$data['smeta']=json_encode($_POST['smeta']);				
				if(!$mlab_model->create($data)){
					$this -> error($mlab_model->geterror());//添加有自动验证规则时，应在此处进行错误提示和判断，不能在最后$result处写，否则报不出来错误！					
				}else{
					$result = $mlab_model->add();
					if($result!==false){
						$this->success("添加成功！",U('Mlabadmin/index'));
					}else{
						$this->error("添加失败！");
					}
					
				}
			}
		}
		
		function edit(){			
			$id = intval(I("get.id"));
			//dump($id);
			$labinfo = D("Lab")->where(array("id"=>$id))->find();
			//dump($labinfo);
			$this -> assign('labinfo',$labinfo);						
			$this -> display();
			//dump($_POST);
			
		}
		
		function edit_post(){
			$id = intval(I("post.id"));
			//dump($id);
			if(!empty($_POST['photos_alt']) && !empty($_POST['photos_url'])){
				foreach ($_POST['photos_url'] as $key=>$url){
					$photourl=sp_asset_relative_url($url);
					$_POST['smeta']['photo'][]=array("url"=>$photourl,"alt"=>$_POST['photos_alt'][$key]);
				}
			}
			$data = I("post.");//获取所有post数据；
			$data['smeta']=json_encode($_POST['smeta']);
			$result = D("Lab")->where(array("id"=>$id))->save($data);
			//dump($result);
			if($result!==false){
				$this->success("修改成功！",U('Mlabadmin/index'));
			}else{
				$this->error("修改失败！");
			}
		}
		
		function delete(){
			$id = intval(I('get.id'));
			$result = D('Lab')->where(array("id"=>$id))->delete();
			if($result!==false){
				$this->success("删除成功！", U("Mlabadmin/index"));
				}else{
				$this->error('删除失败！');
			}
			
		}
		function showinfo(){
			$id = intval(I('get.id'));
			$mlab_model = D('Lab');
			$showinfo = $mlab_model->where(array("id"=>$id))->find();
			$this -> assign('showinfo',$showinfo);			
			$this->display();
			//dump($showinfo['id']);
			
		}
	
}